package com.franklin.icharm;

import java.util.ArrayList;
import java.util.List;

import com.franklin.icharm.MainMenuActivity.Attribute;
import com.franklin.icharm.MainMenuActivity.MyAdapters;
import com.franklin.icharm.MainMenuActivity.MyAdapters.ListItems;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NotificationsActivity extends Activity {

	private ListView lv;
	private ImageView iv;
	MyAdapters adapter;
	private List<Attribute> menu;
	Attribute data[] = new Attribute[] {
			new Attribute(R.drawable.vip_caller,"VIP caller",R.drawable.arrow),
			new Attribute(R.drawable.caller_groups,"Caller groups", R.drawable.arrow),
			new Attribute(R.drawable.other_apps,"Other apps", R.drawable.arrow) };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notifications);
		lv = (ListView) findViewById(R.id.notifications_list);
		iv=(ImageView)findViewById(R.id.exit);
		iv.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
/*				Intent intent=new Intent();
				intent.setClass(NotificationsActivity.this,
						MainMenuActivity.class);
				startActivity(intent);*/
				finish();
				}
		});
		try {

			new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					try {
						menu = parseJson();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					handler.sendEmptyMessage(0);
				}
			}.start();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			adapter = new MyAdapters(NotificationsActivity.this, menu);
			lv.setAdapter(adapter);
//			lv.setOnItemClickListener(listener);
			lv.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					Intent intent = new Intent();
					if(arg2==0)
					{
						intent.setClass(NotificationsActivity.this,
								VIPCallerActivity.class);
						startActivity(intent);
					}
					else if(arg2==1)
					{
						intent.setClass(NotificationsActivity.this,
								CallerGroupsActivity.class);
						startActivity(intent);
					}
					else if(arg2==2)
					{
						intent.putExtra("app", "");
						intent.setClass(NotificationsActivity.this,
								OtherAppsActivity.class);
						startActivity(intent);
					}
				}
			});
		};
	};

	public List<Attribute> parseJson() throws Exception {
		List<Attribute> list = new ArrayList<Attribute>();
		for (int i = 0; i < data.length; i++) {
			Attribute obj = new Attribute();
			obj.SetImage(data[i].GetImage());
			obj.SetPara(data[i].GetPara());
			obj.SetName(data[i].GetName());
			list.add(obj);
		}

		return list;
	}
	
	public class MyAdapters extends BaseAdapter {
		private List<Attribute> currency = null;
		private LayoutInflater linearlayout;

		public MyAdapters(Context con, List<Attribute> listcurrency) {
			linearlayout = LayoutInflater.from(con);
			currency = listcurrency;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return currency.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return currency.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ListItems listitems;
			if (convertView == null) {
				listitems = new ListItems();
				convertView = linearlayout.inflate(R.layout.notifications_items_view, null);
				listitems.name = (TextView) convertView
						.findViewById(R.id.name_1);
				listitems.para = (ImageView) convertView
						.findViewById(R.id.name_2);
				listitems.img = (ImageView) convertView
						.findViewById(R.id.photo_1);
				convertView.setTag(listitems);
			} else {
				listitems = (ListItems) convertView.getTag();
			}
			Attribute cur = currency.get(position);
			listitems.name.setText(cur.GetName());
			listitems.para.setImageResource(cur.GetPara());
			listitems.img.setImageResource(cur.GetImage());
			return convertView;
		}

		class ListItems {
			private ImageView img;
			private TextView name;
			private ImageView para;
		}

	}

	public class Attribute {
		private String Name="";
		private int Para;
		private int Image;
		
		public Attribute() {
			super();
		}

		public Attribute(int image, String name,int para) {
			super();
			this.Image = image;
			this.Name = name;
			this.Para=para;
		}
		public void SetName(String _Name)
		{
			Name = _Name;
		}
		
		public void SetPara(int _Para)
		{
			Para=_Para;
		}
		
		public void SetImage(int _Image)
		{
			Image = _Image;
		}
		
		public String GetName()
		{
			return Name;
		}
		
		public int GetPara()
		{
			return Para;
		}
		
		public int GetImage()
		{
			return Image;
		}
	}

}
